import { Injectable, Logger } from '@nestjs/common';
import { ARTICLE_BROWSER, RedisService } from "../common/service/redis.service";
import { ArticlesService } from "../articles/articles.service";

@Injectable()
export class CacheService {
  constructor(
    private readonly redisService: RedisService,
    private readonly articlesService: ArticlesService,
    private readonly logger: Logger
  ) {
    this.logger = new Logger('CacheService');
  }

  async initArticleViews() {
    try {
      const articles = await this.articlesService.findAllBrowse()
      // 遍历文章数据，存储到Redis中
      for (const article of articles) {
        await this.redisService.set(ARTICLE_BROWSER + article.id, article.browse);
      }
      this.logger.log('初始化文章浏览量数据到Redis完成');
    } catch (error) {
      this.logger.error('初始化文章浏览量数据到Redis时发生错误:', error);
    }
  }
}